EbN0_db = 0:5;
Eb = 1;       //signal energy per bit
EbN0 = 10^(EbN0_db./10);
N0 = Eb./EbN0;         //noise energy
sigma = sqrt(N0./2);

totalBitNum = 5D6;
msgLen = 10;
errBitNum = zeros(EbN0_db);
msgBer_conv = zeros(EbN0_db);

//convolutional code parameters
//the code is 75 convolutional code with 2 shift registers
g = [7, 5];
trel = poly2trellis(2, g);

for j = 1:length(EbN0_db)
  thisSigma = sigma(j);
  for i = 1:totalBitNum/msgLen
    msg = round(rand(msgLen, 1));
    code = convenc(msg, trel);
    code = dec2bin(code, 2);
    code = 2*code - 1; 
    noise = thisSigma*rand(code, 'normal');
    rcv_code = sign(code + noise);
    rcv_code = (rcv_code+1)/2;
    rcv_code = bin2dec(rcv_code);
    rcv_msg = convdec(rcv_code, trel);
    errBitNum(j) = errBitNum(j) + length(find(rcv_msg ~= msg));
  end
end

msgBer_conv = errBitNum./totalBitNum
